package com.bw.feignclient.web;

import com.bw.common.dto.UserDTO;
import com.bw.common.vo.CarVO;
import com.bw.common.vo.ResultVO;
import com.bw.common.vo.UserVO;
import com.bw.feignclient.entity.Car;
import com.bw.feignclient.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RefreshScope
public class ShoppingCarContoller {


    @Autowired
    UserService userService;


    @Value("${foo}")
    private String foo;

    @RequestMapping("/getShoppingCarList")
    public ResultVO getShoppingCarList(HttpServletRequest request){
        log.info("nacos 配置文件中的version 变量为:{}",foo);
        log.info("请求头为:{}",request.getHeader("X-Request-bw"));
        //根据用户id 查询用户的购物车的信息（用户id，商品id，商品数量）
        List<Car> list = new ArrayList<>();
        Car car = new Car(3,4,5);
        Car car1 = new Car(5,5,5);
        list.add(car);
        list.add(car1);
        List<CarVO> carVOList = new ArrayList<>();
        for (Car c : list) {
            CarVO carVO = new CarVO();
            BeanUtils.copyProperties(c,carVO);
            Integer uid = c.getUid();
            //调用 用户服务的接口 根据用户id 获取用户的信息
            UserDTO userDTO = new UserDTO();
            userDTO.setId(uid);
            UserVO userVO = userService.getUserID(userDTO);
            if (userVO == null){
                return ResultVO.fail("500","通过feign 调用接口 查询用户列表出现异常",null);
            }
            carVO.setUname(userVO.getUsername());
            //根据 商品id 调用商品服务  获取商品的信息
            carVO.setSname("");
            carVO.setPrice(null);
            carVO.setTotal(null);//单价*数量
            carVOList.add(carVO);
        }
        return ResultVO.success(carVOList);

    }

}
